<?php
class MYSQLDatabaseProcessor{


	public static $USER_TABEL=array();
	private $db;
	
	function __construct($db,$tables){
	
	}

	const sql_login = "SELECT * FROM %s WHERE(userId='%u' AND password='%f') LIMIT 0,1;";
	public function login($email,$pasw){
		$query_id = $db->query(sprintf($sql_login,$this->USER_TABLE["USERS"],$email,md5($pasw)));
		if(mysql_num_rows($query_id) > 0){
			return $query_id->fetchArray($query_id);
		}
		else{
			return false;
		}
	}

	const sql_register = "INSERT INTO %s (email,name,familyName,password,active) VALUES ('%s','%s','%s','%s','FALSE');";
	public function register($name, $famName, $email, $pasw){
		$query_id= $db->query(sprintf($sql_register,$this->USER_TABLE["USERS"],$email,$name,$famName,md5($pasw)));
		return mysql_insert_id($query_id);
	}
	
	const sql_register = "UPDATE %s SET (level=level+1) WHERE(userId=%u);";
	
	private function _Promovate($userId){
		$query_id= $db->query(sprintf($sql_register,$this->USER_TABLE["USERS"],$email,$name,$famName,md5($pasw)));
		return ($query_id==TRUE);
	}
	
	public function activate($userId,$activationCode){

	}

	public function isValidEmail(){
		//sprintf($this->USER_TABLE["VALID"];
	}
	
	public function isValidName(){
		
	}
	
	public function isValidPasw($pasw){
	}
	
	public static function generateRandomPassword($length = 8){
	  	$password = "";
	  	$possible = "123456789_ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; 
		    
		  $i = 0; 
		  while ($i < $length) { 
		    $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);	
		  }
		  return $password;
	}

	const sql_resetPasw = "UPDATE %s SET (password='%s');";
	private function resetPasw($userID){
		$new_pasw = generateRandomPassword(8);
		$query_id= $db->query(sprintf($sql_resetPasw,$this->USER_TABLE["USERS"],md5($new_pasw)));
		return $new_pasw;
	}
	

}